
//
//  UITextField+Block.m
//  MasonryDemo
//
//  Created by xieyingze on 16/11/9.
//  Copyright © 2016年 xieyingze. All rights reserved.
//

#import "UITextField+Block.h"

@implementation UITextField (Block)

static char overviewKey;

@dynamic event;

- (void)handleControlEvent:(UIControlEvents)controlEvent withBlovk:(InputBlock)inputBlock {
    if (inputBlock) {
        objc_removeAssociatedObjects(self);
        objc_setAssociatedObject(self, &overviewKey, inputBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
        [self addTarget:self action:@selector(inputtingBlock:) forControlEvents:controlEvent];
    }
}

- (void)inputtingBlock:(id)sender {
    if ([sender isKindOfClass:[UITextField class]]) {
        InputBlock block = (InputBlock)objc_getAssociatedObject(self, &overviewKey);
        UITextField *textFiled = (UITextField *)sender;
        if (block) {
            block(textFiled.text);
        }
    }
}

@end
